package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bean.AccountBean;
import com.bean.TradeBean;

public class TradeDao {
	private DBSource db = new SimpleDBSource();
	//添加
	public int addTrade(TradeBean bean) {
		int rows = 0;
		Connection conn = null;
		try{
			conn = db.getConn();
			String sql = "insert into trade(cardno,type,money,tdate) values(?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, bean.getCardno());
			ps.setString(2, bean.getType());
			ps.setFloat(3, bean.getMoney());
			ps.setString(4, bean.getTdate());
			rows = ps.executeUpdate();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			db.closeConn(conn);
		}
		return rows;
	}
	
	//查询全部
		public List<TradeBean> selectAll() {
			Connection conn = null;
			List<TradeBean> trades = new ArrayList<TradeBean>();
			try{
				conn = db.getConn();
				String sql = "select * from trade ";
				PreparedStatement ps = conn.prepareStatement(sql);
				
		
				ResultSet rs = ps.executeQuery();
				while(rs.next())
				{
					TradeBean bean = new TradeBean();
					bean.setCardno(rs.getString("cardno"));
					bean.setTdate(rs.getString("tdate"));
					bean.setMoney(rs.getFloat("money"));
					bean.setType(rs.getString("type"));
					trades.add(bean);
				}
				rs.close();
				ps.close();
			}catch(Exception ex){
				ex.printStackTrace();
			}finally{
				db.closeConn(conn);
			}
			
			return trades;
		}
		
		//查询多个
		public List<TradeBean> getList(String cardno){
			List<TradeBean> list = new ArrayList<TradeBean>();
			TradeBean bean = null;
			Connection conn = null;
			PreparedStatement stat = null;
			ResultSet res = null;
			String sql  = "select * from trade where 1=1";
			if(cardno != null && !"".equals(cardno)){
				sql = sql + " and cardno = "+cardno;
			}
			try {
				conn = db.getConn();
				stat = conn.prepareStatement(sql);
				res = stat.executeQuery();
				while(res.next()){
					bean = new TradeBean();
					bean.setCardno(res.getString("cardno"));
					bean.setType(res.getString("type"));
					bean.setMoney(res.getFloat("money"));
					bean.setTdate(res.getString("tdate"));
					list.add(bean);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				db.closeConn(conn);
			}
			return list;
		}
}
